<?php
require_once '../application/library/controller/gcLBasicAction.php';
require_once '../application/library/exception/gcExceptionDealer.php';
require_once '../application/modules/goods/constant/goods_constant_templatName.php';
require_once '../application/modules/goods/constant/goods_constant_constant.php';
require_once '../application/modules/goods/services/goods_services_goodsFormulaListService.php';


/**
 * 商品一览Controller
 * 商品管理
 * @author    Suda
 * @version   1.0
 */
class goods_goodsFormulaListController extends gcLBasicAction {

    private $objService;
    /**
     * 初期表示
     */
    public function indexAction() {
        try {
            $this->initMenu("1");
            $this->boolUserTypeCheck("1");
            $this->pvSetLabel();
            $this->objService = new goods_services_goodsFormulaListService();
            $intCount = $this->objService->pbGetFormulaCount();
            $objPager=$this->pagerInit($intCount, 1, 10, pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
            $arrFormula = $this->objService->pbGetFormula();
            $arrMsgId = array("ERR-100002");
            $this->setMessageForJs($arrMsgId);
            $this->gcView->intForMulaNum = $intCount;
            $this->gcView->arrFormula = $arrFormula;
            $this->gcView->render(goods_constant_templatName::GOODS_FORMULA_LIST);
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    /**
     * 初期化页面情报
     * pvSetLabel
     * @param null
     * @return null
     */
    private function pvSetLabel() {
        $this->gcView->title                          = $this->getLabel("goods_EC0006_PageTitle");
        $this->gcView->goods_EC0006_H1                = $this->getLabel("goods_EC0006_H1");
        $this->gcView->goods_EC0006_H2                = $this->getLabel("goods_EC0006_H2");
        $this->gcView->goods_EC0006_Unit              = $this->getLabel("goods_EC0006_Unit");
        $this->gcView->goods_EC0006_addFormula        = $this->getLabel("goods_EC0006_addFormula");
        $this->gcView->goods_EC0006_Title2            = $this->getLabel("goods_EC0006_Title2");
        $this->gcView->goods_EC0006_FomulaName        = $this->getLabel("goods_EC0006_FomulaName");
        $this->gcView->goods_EC0006_Search            = $this->getLabel("goods_EC0006_Search");
    }

    /**
     * 检索
     */
    public function searchAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                if ($this->getParam("Flag") == "1") {
                    $strFormulaName = $this->getParam("FromulaName");
                } else {
                    $arrCondition["pageSize"] = $this->getParam("pageSize");
                    $arrCondition["orderColumn"] = $this->getParam("orderColumn");
                    $arrCondition["order"] = $this->getParam("order");
                }
                $this->objService = new goods_services_goodsFormulaListService();
                $intCount = $this->objService->pbGetSearchCount($strFormulaName);
//                var_dump($objPager);exit;
                $objPager = $this->getPagerParam($intCount);
                if ($intCount != 0) {
                    $arrCondition["offset"]      = $objPager->getOffset();
                    $arrCondition["limit"]        = $objPager->getLimit();
                    $CurPage                         = $objPager->getCurPage();

                    $arrFormula = $this->objService->pbSearchFormula($strFormulaName);
                    $arrResault = array(
                        "count"      => $intCount,
                        "curPage"      => $CurPage,
                        "searchData" => $arrFormula
                    );
                } else {
                    $arrResault = array(
                        "count"      => 0,
                        "curPage"    => $objPager->getCurPage(),
                        "searchData" => "");
//                    var_dump($arrData);exit;
                }
                jsonHelper::outPutJson($arrResault);
            } else {
                gcExceptionDealer::throwGcException();
            }
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    public function saveAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $strAddEditFg           = $this->getParam("AddEditFg");
                $arrFormulaGoodsName    = $this->getParam("FormulaGoodsName",true);
                $arrFormulaGoodsNo      = $this->getParam("FormulaGoodsNo",true);
                $arrFormulaGoodsNum     = $this->getParam("FormulaGoodsNum",true);
                $strFormulaName         = $this->getParam("FormulaName");
                $arrProductInfo = array("ProductName" => $this->getParam("ProductName"),
                                        "ProductCode" => $this->getParam("ProductCode"),
                                        "ProductTp"   => $this->getParam("GoodsTp"));
                foreach($arrFormulaGoodsNo as $key => $value) {
                    $arrFormulaGoodsInfo[$key]["GoodsNo"] = $value;
                    $arrFormulaGoodsInfo[$key]["GoodsName"] = $arrFormulaGoodsName[$key ];
                    $arrFormulaGoodsInfo[$key]["Num"] = $arrFormulaGoodsNum[$key ];
                }
                $this->objService = new goods_services_goodsFormulaService();
                if($strAddEditFg == "1") {
                    $strGoodsNo = $this->objService->pbInsertGoods($arrProductInfo);
                    $strFormulaNo = $this->objService->pbInsertFormula($strGoodsNo, $strFormulaName);
                    $this->objService->pbInsertFormulaItem($arrFormulaGoodsInfo, $strFormulaNo);
                } else {
                    $this->objService->pbUpdateFomula();
                    $this->objService->pbUpdateFormulaItem();
                    $this->objService->pbUpdateGoods();
                }
                $output = array();
                jsonHelper::outPutJson($output);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }
}

